package org.jing.ext.example;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.jing.core.util.GenericUtil;
import org.jing.ext.office.ExcelOperator;
import org.jing.ext.office.FontName;

import java.awt.*;
import java.io.File;
import java.lang.Exception;
import java.net.URI;
import java.util.List;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2021-02-26 <br>
 */
public class Example4Office {
    private Example4Office() throws Exception {
        writeExcel();
    }

    private void writeExcel() throws Exception {
        File excelFile = new File("logs/temp.xlsx");
        Workbook excel = ExcelOperator.createExcel(excelFile);
        Sheet sheet = excel.createSheet("sheet1");
        // 文本格式
        final short stringFormat = ExcelOperator.createDataFormat(excel, "@");
        ExcelOperator.CellStyle headerStyle = ExcelOperator
            .createCellStyle(excel).setFontNameAndSize(FontName.YaHei, 10).setBold(true)
            .setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.CENTER).setCellFormat(stringFormat)
            .setAllBorder(BorderStyle.MEDIUM);
        ExcelOperator.CellStyle childStyle = ExcelOperator
            .createCellStyle(excel).setFontNameAndSize(FontName.YaHei, 10).setBold(true)
            .setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.CENTER).setCellFormat(stringFormat)
            .setLeftBorder(BorderStyle.THIN).setRightBorder(BorderStyle.THIN).setBottomBorder(BorderStyle.THIN);

        int rowIndex = -1;
        int colIndex;
        Row row;
        List<String> eventList = GenericUtil.<String>buildList().add("06-18").add("11-11").add("12-12").get();
        String[] columnArr = {"商品名", "最新", "最低", "最高", "已收回垫款笔数", "已收回垫款金额"};
        List<String> columnList = GenericUtil.<String>buildList().add("商品名").add("最新").add("最低").add("最高").get();
        int count = eventList.size();
        for (int i$ = 0; i$ < count; i$++) {
            columnList.add("电商节" + (i$ + 1));
        }
        columnList.add("关注价格");
        columnList.add("链接");
        int colCount = columnList.size();
        int[] maxWidthArr = new int[colCount];

        // 设置列宽
        sheet.setColumnWidth(0, 13000);
        sheet.setColumnWidth(1, 2000);

        // row: 1
        rowIndex ++;
        colIndex = 0;
        row = sheet.createRow(rowIndex);
        row.setHeight((short) 700);
        String column;
        for (int i$ = 0; i$ < colCount; i$++) {
            column = columnList.get(i$);
            if (i$ == 0) {
                ExcelOperator.mergeCell(sheet, 0, 0, 0, 1);
                headerStyle.add2Cell(row.createCell(i$)).getBase().setCellValue(column);
            }
            headerStyle.add2Cell(row.createCell(i$ + 1)).getBase().setCellValue(column);
        }

        // row: 2~n
        int rowIndex$1, rowIndex$2, rowIndex$3;
        Row row$1, row$2, row$3;
        for (int i$ = 0; i$ < 3; i$++) {
            rowIndex$1 = i$ * 3 + 1;
            rowIndex$2 = i$ * 3 + 2;
            rowIndex$3 = i$ * 3 + 3;
            ExcelOperator.mergeCell(sheet, rowIndex$1, rowIndex$3, 0, 0);
            row$1 = sheet.createRow(rowIndex$1);
            row$1.setHeight((short) 300);
            row$2 = sheet.createRow(rowIndex$2);
            row$2.setHeight((short) 300);
            row$3 = sheet.createRow(rowIndex$3);
            row$3.setHeight((short) 300);
            // 商品名
            childStyle.add2Cell(row$1.createCell(0)).getBase().setCellValue("asdsaasfsdgfsdagdsgasdgasdgasdasdfasdfadsf");
            //  日期
            childStyle.add2Cell(row$1.createCell(1)).getBase().setCellValue("日期");
            childStyle.add2Cell(row$2.createCell(1)).getBase().setCellValue("价格");
            childStyle.add2Cell(row$3.createCell(1)).getBase().setCellValue("活动");
        }

        for (int i$ = 0; i$ < colCount; i$++) {
            if (maxWidthArr[i$] != 0) {
                sheet.setColumnWidth(i$, maxWidthArr[i$] * 256 + 200);
            }
        }
        ExcelOperator.writeExcel(excel, excelFile);

        URI uri = new URI("file:///" + excelFile.getAbsolutePath().replaceAll("\\\\", "/"));
        Desktop.getDesktop().browse(uri);
    }

    public static void main(String[] args) throws Exception {
        new Example4Office();
    }
}
